21. Splatting

Note

The below information is extensively based in information taken from the PowerShell® Notes for Professionals book. I plan to extend this information based on my day to day usage of the language.

Splatting is a method of passing multiple parameters to a command as a single unit. This is done by storing the parameters and their values as key-value pairs in a hashtable and splatting it to a cmdlet using the splatting operator @.

Splatting can make a command more readable and allows you to reuse parameters in multiple command calls.

21.1: Piping and Splatting

Declaring the splat is useful for reusing sets of parameters multiple times or with slight variations:

1
2
3
4
5
$splat = @{
  Class = "Win32_SystemEnclosure"
  Property = "Manufacturer"
  ErrorAction = "Stop"
}
1
2
3
Get-WmiObject -ComputerName $env:COMPUTERNAME @splat
Get-WmiObject -ComputerName "Computer2" @splat
Get-WmiObject -ComputerName "Computer3" @splat

However, if the splat is not indented for reuse, you may not wish to declare it. It can be piped instead:

1
2
3
4
5
6
@{
    ComputerName = $env:COMPUTERNAME
    Class = "Win32_SystemEnclosure"
    Property = "Manufacturer"
    ErrorAction = "Stop"
} | % { Get-WmiObject @_ }

21.2: Passing a Switch parameter using Splatting

To use Splatting to call Get-Process with the -FileVersionInfo switch similar to this:

1
Get-Process - FileVersionInfo

This is the call using splatting:

1
2
3
$MyParameters = @{
  FileVersionInfo = $true
}
1
Get-Process @MyParameters

Note: This is useful because you can create a default set of parameters and make the call many times like this

1
2
3
$MyParameters = @{
  FileVersionInfo = $true
}
1
2
Get-Process @MyParameters -Name WmiPrvSE
Get-Process @MyParameters -Name explorer

21.3: Splatting From Top Level Function to a Series of Inner Function

Without splatting it is very cumbersome to try and pass values down through the call stack. But if you combine splatting with the power of the @PSBoundParameters then you can pass the top level parameter collection down through the layers.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Function Outer-Method
{
  Param
  (
    [string]
    $First,
    [string]
    $Second
  )
  Write-Host ($First) -NoNewline
  Inner-Method @PSBoundParameters
}
Function Inner-Method
{
  Param
  (
    [string]
    $Second
  )
  Write-Host (" {0}!" -f $Second)
}
$parameters = @{
  First = "Hello"
  Second = "World"
}
Outer-Method @parameters

21.4: Splatting parameters

Splatting is done by replacing the dollar-sign $ with the splatting operator @ when using a variable containing a

HashTable of parameters and values in a command call.

1
2
3
4
5
6
$MyParameters = @{
  Name = "iexplore"
  FileVersionInfo = $true
}

Get-Process @MyParameters

Without splatting:

1
Get-Process -Name "iexplore" - FileVersionInfo

You can combine normal parameters with splatted parameters to easily add common parameters to your calls.

1
2
3
$MyParameters = @{
  ComputerName = "StackOverflow-PC"
}
1
2
Get-Process -Name "iexplore" @MyParameters
Invoke-Command -ScriptBlock { "Something to execute remotely" } @MyParameters